package com.gouaixin.bee.beeframework2.model;

import com.google.gson.Gson;
import com.gouaixin.bee.beeframework2.tool.GetClassInfo;
import com.gouaixin.bee.beeframework2.tool.XHttpUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

/**
 * Created by Administrator on 2015/4/1.
 */
public class HttpModel {

    static String mTAG="";


    public static void post(final String url,RequestParams params) {
        mTAG = url;
//        mLog.e("执行网络请求:url="+mTAG);
        final String[] result = {""};

        Object listss = GetClassInfo.getValueByName("bodyParams", params);
        final String request=new Gson().toJson(listss);
        mLog.e(mTAG + "：请求=" + request);

        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST,
                url,
                params,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        mLog.e(mTAG + "：响应=" + XHttpUtils.Unicode2Chinese(responseInfo.result));
                        result[0] = XHttpUtils.Unicode2Chinese(responseInfo.result);
                        if(mCallBackLister!=null){
                            if(!result[0].equals("null")&&result[0]!=null&&!result[0].equals("")){
                                mCallBackLister.onCallBack(url,result[0]);
                            }
                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        mLog.e(mTAG + "：请求故障=" + msg);
                    }
                }
        );
    }


    public static void get(final String url) {
        mTAG = url;
        mLog.e("执行网络请求:url=" + mTAG);
        final String[] result = {""};


        mLog.e(mTAG + "：请求=" + url);

        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.GET,
                url,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        mLog.e(mTAG + "：响应=" + XHttpUtils.Unicode2Chinese(responseInfo.result));
                        result[0] = XHttpUtils.Unicode2Chinese(responseInfo.result);
                        if(mCallBackLister!=null){
                            if(!result[0].equals("null")&&result[0]!=null&&!result[0].equals("")){
                                mCallBackLister.onCallBack(url,result[0]);
                            }

                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        mLog.e(mTAG + "：请求故障=" + msg);
                    }
                }
        );


    }

    //自定义一个回调方法
    /**
     * 定义一个接口
     */
    public interface CallBackLister{
        public void onCallBack(String url, String result);
    }

    /**
     * 初始化接口变量
     */
    public static CallBackLister mCallBackLister = null;

    /**
     * 自定义控件的自定义事件
     */
    public static void setCallBackLister(CallBackLister myCallBackLister)
    {
       mCallBackLister = myCallBackLister;
    }
}
